<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Incentive extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('admin_id'), $this->session->userdata('permission_verify'));
        $this->data['controller'] = 'incentive';
        $this->load->model('Mincentive');
        $this->_per = $this->checkPerMission($this->session->userdata('admin'), $this->session->userdata('SessionPermisson'), 17, $this->session->userdata('admin_id'));
    }    
    
    public function incentives()
    {
        if($this->_per->read != 1)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->load->library('pagination');
        $config['per_page'] = 10;
        $this->data['page'] = $this->data['pageCurrent'] = $this->getParamUri(4,3);
        $offset = (($this->getParamUri(4,3) - 1) * $config['per_page']) > 0 ? (($this->getParamUri(4,3) - 1) * $config['per_page']) : 0;
        $data = $this->Mincentive->dbincentives($config['per_page'],$offset);
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2).'/'.$this->uri->segment(3).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 4;
        $config['uri_segment_page'] = $this->getParamUri(4,3);
        $config['suffix'] = '.html';
        $config['use_page_numbers'] = TRUE;
        $this->pagination->initialize($config);
        $this->data['data'] = $data['data'];
        
        $this->data['action'] = 'incentives';
        $this->load->view('template/layout', $this->data);
    }
    
    public function addincentives()
    {
        if($this->_per->add != 2)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['ecoupon'] = $this->Mincentive->selecteCoupon();
        
        /*control*/
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('title', 'Tên ưu đãi', 'required');
        $this->form_validation->set_rules('img_incens_hd_', 'Ảnh ưu đãi', 'required');
        $this->form_validation->set_rules('img1_incens_hd_', 'Ảnh bài viết', 'required');
        $this->form_validation->set_rules('summary', 'Mô tả', 'required');
        $this->form_validation->set_rules('content', 'Nội dung', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $title = $this->getParamString('title');
            $dataimage = $this->getParamString('img_incens_hd_');//die($dataimage);
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'incentives-'.$timenow.'.jpg';
                $path = 'images/incentives/'.date('Ym').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$dataimage);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }
            
            /* Ảnh bài viết*/
            $dataimage1 = $this->getParamString('img1_incens_hd_');//die($dataimage);
            if(!in_array($dataimage1, array(null, '', '0')))
            {
                $timenow1 = time().rand(0,9999);
                $nameimagess1 = 'incentives-'.$timenow1.'.jpg';
                $path1 = 'images/incentives/'.date('Ym').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
                $paths1 = PROJECT_PATH.'/data/'.$path1;
                $arr_exp1 = array('[removed]', '[removed');
                $exp1 = str_replace($arr_exp1,'',$dataimage1);
                $this->savefile($exp1,$paths1,$nameimagess1);
                $img1 = $path1.$nameimagess1;
            }else{
                $img1 = '';
            }
            
            $summary = $this->getParamStringAdmin('summary');
            $content = $this->getParamStringAdmin('content');
            $ecoupon = $this->getParamInt('sl_coupon');
            
            $this->data['return'] = $this->Mincentive->dbaddincentives($title,$img,$summary,$content,$this->session->userdata('admin_id'),$img1,$ecoupon);
            if($this->data['return'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/incentive/add-incentives";</script>');
            }
        }
         
        $this->data['action'] = 'addincentives';
        $this->load->view('template/layout', $this->data);
    }
    
    private function select_month()
    {
        $data = array(
            '1' => 'Tháng 1',
            '2' => 'Tháng 2',
            '3' => 'Tháng 3',
            '4' => 'Tháng 4',
            '5' => 'Tháng 5',
            '6' => 'Tháng 6',
            '7' => 'Tháng 7',
            '8' => 'Tháng 8',
            '9' => 'Tháng 9',
            '10' => 'Tháng 10',
            '11' => 'Tháng 11',
            '12' => 'Tháng 12',
        );
        return $data;
    }
    private function select_year()
    {
        $start = 2013;
        $data = array();
        for($start;$start <= 2100;$start++)
        {
            $data[$start] = 'Năm '.$start;   
        }
        return $data;
    }
    
    public function editincentives()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $this->data['ecoupon'] = $this->Mincentive->selecteCoupon();
        
        $this->data['id'] = $this->security->xss_clean($this->getParamUri(4,1));
        $this->data['data'] = $this->Mincentive->dbgetinfoincentives($this->data['id']);
        $this->data['return'] = 0;
        
        /*control*/
        $this->load->library('form_validation');        
        $this->form_validation->set_rules('title', 'Tên ưu đãi', 'required');
        $this->form_validation->set_rules('img_incens_hd_check', 'Ảnh ưu đãi', 'required');
        $this->form_validation->set_rules('img1_incens_hd_check', 'Ảnh bài viết', 'required');
        $this->form_validation->set_rules('summary', 'Mô tả', 'required');
        $this->form_validation->set_rules('content', 'Nội dung', 'required');
        
        if($this->form_validation->run() === TRUE)
        {
            $title = $this->getParamString('title');
            $dataimage = $this->getParamString('img_incens_hd_');//die($dataimage);
            
            $dataimage_check = $this->getParamString('img_incens_hd_check');
            $verify_image = $this->getParamString('img_incens_verify');
            if(!in_array($verify_image, array(null, '')))
            {
                if($verify_image != $dataimage_check)
                {
                    @unlink(PROJECT_PATH.'/data/'.$verify_image);
                }
            }
            
            if(!in_array($dataimage, array(null, '', '0')))
            {
                $timenow = time().rand(0,9999);
                $nameimagess = 'incentives-'.$timenow.'.jpg';
                $path = 'images/incentives/'.date('Ym').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path, 0301);
                $paths = PROJECT_PATH.'/data/'.$path;
                $arr_exp = array('[removed]', '[removed');
                $exp = str_replace($arr_exp,'',$dataimage);
                $this->savefile($exp,$paths,$nameimagess);
                $img = $path.$nameimagess;
            }else{
                $img = '';
            }
            
            /* Ảnh bài viết */
            $dataimage1 = $this->getParamString('img1_incens_hd_');//die($dataimage);
            
            $dataimage_check1 = $this->getParamString('img1_incens_hd_check');
            $verify_image1 = $this->getParamString('img1_incens_verify');
            if(!in_array($verify_image1, array(null, '')))
            {
                if($verify_image1 != $dataimage_check1)
                {
                    @unlink(PROJECT_PATH.'/data/'.$verify_image1);
                }
            }
            
            if(!in_array($dataimage1, array(null, '', '0')))
            {
                $timenow1 = time().rand(0,9999);
                $nameimagess1 = 'incentives-'.$timenow1.'.jpg';
                $path1 = 'images/incentives/'.date('Ym').'/';
                @mkdir(PROJECT_PATH.'/data/'.$path1, 0301);
                $paths1 = PROJECT_PATH.'/data/'.$path1;
                $arr_exp1 = array('[removed]', '[removed');
                $exp1 = str_replace($arr_exp1,'',$dataimage1);
                $this->savefile($exp1,$paths1,$nameimagess1);
                $img1 = $path1.$nameimagess1;
            }else{
                $img1 = '';
            }
            
            $summary = $this->getParamStringAdmin('summary');
            $content = $this->getParamStringAdmin('content');
            $ecoupon = $this->getParamInt('sl_coupon');
                        
            $ids = $this->data['id'];
            $this->data['return'] = $this->Mincentive->dbeditincentives($ids,$title,$img,$summary,$content,$this->session->userdata('admin_id'),$img1,$ecoupon);
            if($this->data['return'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Lưu thành công");window.location.href="'.$this->data['baseurl'].'manage/incentive/edit-incentives/'.$ids.'";</script>');
            }
        }
        
        $this->data['action'] = 'editincentives';
        $this->load->view('template/layout', $this->data);
    }
    
    public function activeincentives()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $id = $this->getParamInt('id');
        die(json_encode($this->Mincentive->dbactiveincentives($id)));
    }
    
    public function deleteincentives()
    {
        if($this->_per->del != 4)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $id = $this->getParamInt('id');
        die(json_encode($this->Mincentive->dbdeleteincentives($id)));
    }
    
    
    public function deleteimagesajax()
    {
        if($this->_per->edit != 3)
        {
            $this->redirect_notification($this->data['page'],array('Thông Báo', '<center><strong>Bạn không có quyền này</strong></center>'),'thong-bao', 'index');
            exit();
        }
        
        $path = $this->getParamString('path');
        if($path != 1)
        {
            @unlink(PROJECT_PATH.'/data/'.$path);
        }
    }
}
